package com.lwc.cfdns.controller.admin;

import com.lwc.cfdns.annotation.OperLog;
import com.lwc.cfdns.config.AppConfig;
import com.lwc.cfdns.config.AttachmentLocalConfig;
import com.lwc.cfdns.constants.ConstantsUrl;
import com.lwc.cfdns.pojo.dto.ValidGroup;
import com.lwc.cfdns.pojo.dto.system.AdminLoginDTO;
import com.lwc.cfdns.pojo.vo.system.AdminLoginVO;
import com.lwc.cfdns.pojo.vo.system.AdminVO;
import com.lwc.cfdns.pojo.vo.system.MenuOwnVO;
import com.lwc.cfdns.pojo.vo.system.MenuVO;
import com.lwc.cfdns.service.impl.SystemAdminServiceImpl;
import com.lwc.cfdns.service.impl.SystemMenuServiceImpl;
import com.lwc.cfdns.service.impl.SystemRoleServiceImpl;
import com.lwc.cfdns.utils.ResultUtils;
import com.lwc.cfdns.utils.SpringContextUtils;
import com.lwc.cfdns.utils.file.StorageUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.web.servlet.MultipartProperties;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * 通用接口
 *
 * @author luwc
 * @title PublicController
 * @description
 * @time 2023/4/29 6:31
 * @version: 1.0
 */
@RestController
@RequestMapping(ConstantsUrl.ADMIN + "/common")
public class CommonController {
    private static final Logger logger = LoggerFactory.getLogger(CommonController.class);
    @Autowired
    private SystemAdminServiceImpl systemAdminService;
    @Autowired
    private SystemMenuServiceImpl systemMenuService;

    /**
     * 后台登录
     *
     * @param adminLoginDTO
     * @return AdminLoginVO
     * @throws
     * @version 1.0.0
     * @author luwc
     * @time 2023/4/29 6:43
     */

    @OperLog(title = "系统登录测试手动写日志")
    @PostMapping("/login")
    public AdminLoginVO login(@Validated(ValidGroup.Login.class) @RequestBody AdminLoginDTO adminLoginDTO) {
        return systemAdminService.login(adminLoginDTO);
    }

    /**
     * 管理员详情及权限
     *
     * @param
     * @return Map<String, Object>
     * @throws
     * @version 1.0.0
     * @author luwc
     * @time 2023/5/26 17:39:25
     */
    @GetMapping("/admininfo")
    public Map<String, Object> adminInfo() {
        Map<String, Object> map = new HashMap<>();
        List<String> permissions = new ArrayList<>();
        permissions.add("*:*:*"); // todo 需要读取角色表
        map.put("permissions", permissions);
        AdminVO adminVO = systemAdminService.loginView();
        map.put("roleList", adminVO.getRoleList());
        map.put("stationList", adminVO.getStationList());
        map.put("deptList", adminVO.getDeptList());
        map.put("user", adminVO);
        return map;
    }

    /**
     * 我的权限菜单
     *
     * @param
     * @return List<MenuVO>
     * @throws
     * @version 1.0.0
     * @author luwc
     * @time 2023/5/26 18:47:53
     */
    @GetMapping("/byown")
    public List<MenuOwnVO> byown() {
        return systemMenuService.byown();
    }

    /**
     * 退出登录
     *
     * @param
     * @return void
     * @throws
     * @version 1.0.0
     * @author luwc
     * @time 2023/5/16 14:49:03
     */
    @PostMapping("/logout")
    public ResultUtils logout() {
        systemAdminService.logout();
        return ResultUtils.success("注销成功!");
    }

    /**
     * 上传
     * 测试用。。
     *
     * @param uploadFile
     * @param request
     * @return Map<String, Object>
     * @throws
     * @version 1.0.0
     * @author luwc
     * @time 2023/6/2 12:30:08
     */
    @PostMapping("/upload/test")
    public Map<String, Object> uploadTest(MultipartFile uploadFile, HttpServletRequest request) {
        // 临时目录
//        String path = request.getSession().getServletContext().getRealPath("upload/");
        // 指定目录
        String path = SpringContextUtils.getBean(MultipartProperties.class).getLocation();

        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd");
        String format = dateFormat.format(new Date());
        File folder = new File(path + format);
        Map<String, Object> map = new HashMap<>();
        if (!folder.isDirectory()) {
            folder.mkdirs();
        }
        logger.info("文件存储路径：{},保存目录：{}", path, path + format);
        String oldName = uploadFile.getOriginalFilename();
        String newName = UUID.randomUUID().toString() + oldName.substring(oldName.lastIndexOf("."), oldName.length());
        try {
            uploadFile.transferTo(new File(folder, newName));
            map.put("url", request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + AttachmentLocalConfig.getUploadPath() + format + "/" + newName);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return map;
    }

    /**
     * 上传文件
     *
     * @param uploadFile
     * @param request
     * @return Map<String, Object>
     * @throws
     * @version 1.0.0
     * @author luwc
     * @time 2023/6/4 11:23
     */
    @PostMapping("/uploadFile")
    public Map<String, Object> uploadFile(MultipartFile uploadFile, HttpServletRequest request) {
        Map<String, Object> map = new HashMap<>();
        map.put("url", StorageUtils.uploadFile(uploadFile));
        return map;
    }

    /**
     * 上传图片
     *
     * @param uploadFile
     * @param request
     * @return Map<String, Object>
     * @throws
     * @version 1.0.0
     * @author luwc
     * @time 2023/6/4 11:23
     */
    @PostMapping("/uploadImage")
    public Map<String, Object> uploadImage(MultipartFile uploadFile, HttpServletRequest request) {
        Map<String, Object> map = new HashMap<>();
        map.put("url", StorageUtils.uploadImage(uploadFile));
        return map;
    }

    /**
     * 上传图片及文件
     *
     * @param uploadFile
     * @param request
     * @return Map<String, Object>
     * @throws
     * @version 1.0.0
     * @author luwc
     * @time 2023/6/4 11:23
     */
    @PostMapping("/upload")
    public Map<String, Object> upload(MultipartFile uploadFile, HttpServletRequest request) {
        Map<String, Object> map = new HashMap<>();
        map.put("url", StorageUtils.upload(uploadFile));
        return map;
    }
}
